import { Controller, Get, Post, Put, Del, Inject, Body, Param, Query } from '@midwayjs/decorator';
import { ActionLogOtaService } from '../../service/firmware/actionLogOta.service';
import { resBuild } from '@utils/resBuild';
import { actionLogOtaDto } from '@dto/firmware/action_log_ota.dto';
@Controller('/action_log_ota')
export class ActionLogOtaController {
  @Inject()
  actionLogService: ActionLogOtaService;


  // **新增 OTA 操作日志**
  @Post('/create')
  async create(@Body() body: actionLogOtaDto) {
    try {
      const result = await this.actionLogService.createActionLog(body);
      console.log('result', result)
      console.log('任务创建成功')
      return resBuild.success();
    } catch (error) {
      console.log('err0', error)
      return resBuild.fail('新增任务失败', error);
    }
  }

  // **获取所有 OTA 操作日志**
  @Get('/list')
  async list(@Query() param) {
    try{
      const logs = await this.actionLogService.getAllActionLogs(param);
      return resBuild.data(logs);
    }catch(err){
      console.log(11111,err)
      return resBuild.fail(err.message)
    }
    
  }


  // **获取单个 OTA 操作日志**
  @Get('/:id')
  async getById(@Param('id') id: number) {
    try{
      const log = await this.actionLogService.getActionLogById(id);
      return resBuild.data(log)
    }catch(err){
      console.log('err',err.message)
      return resBuild.fail(err.message)
    }
  }

  // **根据网关和手环 MAC 获取 OTA 操作日志**
  @Get('/list_gw_wb')
  async getByMacs(@Query() param) {
    try{
      const logs = await this.actionLogService.getActionLogsByGwAndWb(param);
      return resBuild.data(logs);
    }catch(err){
      console.log('action_log err', err);
      return resBuild.fail(err.message)
    }
  }

  // **更新 OTA 操作日志**
  @Put('/update/:id')
  async update(@Param('id') id: number, @Body() body) {
    try {
      const updatedLog = await this.actionLogService.updateActionLog(id, body);
      if (!updatedLog) return { success: false, message: '更新失败，日志不存在' };
      return { success: true, data: updatedLog };
    } catch (error) {
      return { success: false, message: error.message };
    }
  }

  // **删除 OTA 操作日志**
  @Del('/delete/:id')
  async delete(@Param('id') id: string) {
    const deleted = await this.actionLogService.deleteActionLog(id);
    if (!deleted) return { success: false, message: '删除失败，日志不存在' };
    return { success: true, message: '删除成功' };
  }
}
